package com.team.game.Screens;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;

import com.team.game.Assets;
import com.team.game.Game.Gestures;
import com.team.game.framework.Game;
import com.team.game.framework.Level;
import com.team.game.framework.Screen;
import com.team.game.framework.impl.SurfaceThread;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;

import java.io.IOException;
import java.io.InputStream;

import static com.team.game.framework.impl.Scale_X_Y.Scale_X;
import static com.team.game.framework.impl.Scale_X_Y.Scale_Y;

public class Setting_Screen extends Screen {

    @SuppressWarnings("unused")
    private static final String TAG = "Settings_Screen";

    private RectF rectBackground;
    private Bitmap background;

    private final RectF rectSoundOn;
    private final RectF rectSoundOff;

    private final RectF rectSound;
    private final RectF rectSound1;

    private Bitmap bmpSoundOn, bmpSoundOff;

    private final Paint paint, paint1;

    private boolean state = false;

    public Setting_Screen (Game game) {
        //Вызов родительского метода
        super(game);

        SurfaceThread.wait = true;

        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open("Picture/sett.jpg");
            background = BitmapFactory.decodeStream(inputStream);

            inputStream = MainActivity.manager.open ("Picture/sound_on.png");
            bmpSoundOn = BitmapFactory.decodeStream (inputStream);

            inputStream = MainActivity.manager.open ("Picture/sound_off.png");
            bmpSoundOff = BitmapFactory.decodeStream (inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }

        //Фон
        rectBackground = new RectF (0, 0, Scale_X_Y.Size_X, Scale_X_Y.Size_Y);
        //фон для настроек громкости
        rectSound = new RectF (100 * Scale_X, 100 * Scale_Y, 980 * Scale_X, 500 * Scale_Y);
        //Размеры картинки включения звука
        rectSoundOn = new RectF (200 * Scale_X, 200 * Scale_Y, 400 * Scale_X, 400 * Scale_Y);
        //Размеры картинки выключения звука
        rectSoundOff = new RectF (680 * Scale_X, 200 * Scale_Y, 880 * Scale_X, 400 * Scale_Y);

        // Размеры для выделения активной настройки звука
        rectSound1 = new RectF ();

        paint = new Paint();
        paint.setColor (Color.WHITE);

        paint1 = new Paint ();
        paint1.setColor (Color.LTGRAY);

        SurfaceThread.wait = false;
    }

    @Override
    public boolean touchEvent (MotionEvent motionEvent) {

        //Проверка какая кнопка нажата
        if (Assets.inRect (rectSoundOn, motionEvent))
            state = true;
        else if (Assets.inRect (rectSoundOff, motionEvent))
            state = false;

        return false;
    }

    @Override
    public void update() {

        if (state) { //если нажата кнопка вкл звук
            rectSound1.set (rectSoundOn.left - 30 * Scale_X,
                    rectSoundOn.top - 30 * Scale_Y,
                    rectSoundOn.right + 30 * Scale_X,
                    rectSoundOn.bottom + 30 * Scale_Y);
        } else { // если нажата кнопка выкл звук
            rectSound1.set (rectSoundOff.left - 30 * Scale_X,
                    rectSoundOff.top - 30 * Scale_Y,
                    rectSoundOff.right + 30 * Scale_X,
                    rectSoundOff.bottom + 30 * Scale_Y);
        }
    }

    @Override
    public void present(Canvas canvas) {
        //фон
        canvas.drawBitmap (background, null, rectBackground, null);
        //фон для настроек звука
        canvas.drawRoundRect (rectSound, 30 * Scale_X, 30 * Scale_Y, paint);
        //подцветка выбранной настройки звука
        canvas.drawRoundRect (rectSound1, 30 * Scale_X, 30 * Scale_Y, paint1);
        //отрисовка кнопок вкл/выкл
        canvas.drawBitmap (bmpSoundOn, null, rectSoundOn, null);
        canvas.drawBitmap (bmpSoundOff, null, rectSoundOff, null);
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        background.recycle();
        rectBackground = null;
    }

    @Override
    public void back() {
        game.setScreen (new Main_Screen (game));
    }

    @Override
    public Level getLevel() {
        return null;
    }

    @Override
    public Gestures getGestures() {
        return null;
    }

}
